package com.youth.controller;

import com.youth.AddGroup;
import com.youth.api.UserApi;
import com.youth.entity.po.User;
import com.youth.result.ResultBean;
import com.youth.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author: wuxian
 * @date: 2020/1/7 9:41
 */
@RestController
@RequestMapping("user")
@Api(tags = "用户")
@Slf4j
@Validated
public class UserController implements UserApi {

    @Resource
    private UserService userService;

    /**
     * @param userName
     * @param password
     * @return com.youth.result.ResultBean
     * @Description 登录
     * @author wuxian
     * @date 2020/1/7 10:01
     */
    @Override
    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String", required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", required = true)
    })
    @GetMapping("login")
    public ResultBean login(@NotBlank(message = "用户名不能为空") @RequestParam("userName") String userName,
                            @NotBlank(message = "密码不能为空") @RequestParam("password") String password) {
        log.info("用户登录 用户名 {} 密码 {}", userName, password);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        subject.login(token);
        return new ResultBean(200, "登录成功", null);
    }

    /**
     * @param user
     * @return com.youth.result.ResultBean
     * @Description 注册
     * @author wuxian
     * @date 2020/1/7 10:01
     */
    @Override
    @ApiOperation("注册")
    @PostMapping("register")
    public ResultBean register(@Validated(value = {AddGroup.class}) @RequestBody User user) {
        log.info("用户注册 {}", user.toString());
        int i = userService.register(user);
        if (i > 0) {
            return new ResultBean(200, "注册成功", null);
        } else {
            return new ResultBean(500, "注册失败", null);
        }
    }

    /**
     * 登出
     *
     * @return
     */
    @Override
    @ApiOperation("登出")
    @GetMapping("logout")
    public ResultBean logout() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        subject.logout();
        log.info("用户 {} 登出", user.getUsername());
        return new ResultBean(200, "登出成功", null);
    }


    @Override
    @GetMapping("getUserPressionByName")
    public ResultBean getUserPressionByName(@RequestParam("userName") String userName) {
        List<String> pression = userService.getUserPressionByName(userName);
        return ResultBean.returnSelectSuccess(pression);
    }

    @Override
    @GetMapping("getUserRolesByName")
    public ResultBean getUserRolesByName(@RequestParam("userName") String userName) {
        List<String> roles = userService.getUserRolesByName(userName);
        return ResultBean.returnSelectSuccess(roles);
    }
}
